<?php
/**
 * @author: Gardennet
 * @date: 18-07-24
 * @email: lwp8800@sina.com
 */

namespace App\Services\Payment;

use App\Services\Payment\PayAdapter\WechatPayAdapter;
use App\Services\Payment\PayAdapter\ZhongxinPayAdapter;
use App\Helpers\SnHelper;

class PaymentService extends AbstractPayment implements PaymentInterface
{
    /**
     * 支付
     *
     * @param array $order
     * $order['pay_type']     支付类型  1 现金支付 2 微信子商户支付 默认现金
     *
     * $order['product_id']   商品id  传平台用户id
     * $order['body']         支付描述
     * $order['total_fee']    总金额(元)
     * $order['trade_type']   交易类型 JSAPI(公众号支付) NATIVE(扫码支付)
     * $order['openId']       用户openid 交易类型为JSAPI不能为空
     * $order['attach']       校验数据  格式mid:type
     *
     * @return array
     */
    public function pay($order)
    {
        $_start_time = SnHelper::getMillisecond(0);

        $wechatType = config('payment.wechat_type');
        $wechatPayAdapter = new WechatPayAdapter();
        $zhongxinPayAdapter = new ZhongxinPayAdapter();

        switch($order['pay_type']){
            case self::PAY_CASH://现金
                if($wechatType == self::WECHATTYPE_WECHAT){
                    $start_time = SnHelper::getMillisecond(0);
                    $order['is_sub_pay'] = false;
                    $pay = $wechatPayAdapter->pay($order);
                    $end_time = SnHelper::getMillisecond(0);
                    $this->payTimeNote('wechat',$start_time,$end_time,$order);

                }elseif($wechatType == self::WECHATTYPE_ZHONGXIN){
                    $start_time = SnHelper::getMillisecond(0);
                    $pay = $zhongxinPayAdapter->pay($order);
                    $end_time = SnHelper::getMillisecond(0);
                    $this->payTimeNote('zhongxin',$start_time,$end_time,$order);

                }
                break;
            case self::PAY_SUB_CASH://微信子商户支付
                $start_time = SnHelper::getMillisecond(0);
                $order['is_sub_pay'] = true;
                $pay = $wechatPayAdapter->pay($order);
                $end_time = SnHelper::getMillisecond(0);
                $this->payTimeNote('wechat',$start_time,$end_time,$order);
        }

        $_end_time = SnHelper::getMillisecond(0);
        $this->payTimeNote('',$_start_time,$_end_time,$order);

        return $pay;
    }

    public function refund($order)
    {

    }
}